﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EncodingConvert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string[] pstrUsedEncList = new string[11] { "gb2312", "gb18030", "big5",
            "shift_jis", "euc-jp", "us-ascii", "utf-7", "utf-8", "utf-16", "utf-32", "unicodefffe" };

        private void Form1_Load(object sender, EventArgs e)
        {
            int i;
            System.Text.EncodingInfo[] encinfEncode;
            encinfEncode = System.Text.Encoding.GetEncodings();

            for (i = 0; i < encinfEncode.Length; i++)
            {
                cmbEncList.Items.Add(encinfEncode[i].Name.ToLower());
            }

            cmbInput.Items.AddRange(pstrUsedEncList);
            cmbOutputEnc.Items.AddRange(pstrUsedEncList);
            txtInput.Focus();
        }

        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.F2)
            {
                return;
            }
            txtOutput.Text = "";
            if (txtInput.Text.Trim() == string.Empty)
            {
                return;
            }

            string strEnc;

            strEnc = cmbInput.Text.Trim();
            if (strEnc == string.Empty)
            {
                txtOutput.Text = "Please select the encoding of input.";
                cmbInput.Focus();
                return;
            }
            if (!cmbEncList.Items.Contains(strEnc))
            {
                txtOutput.Text = "input encoding wrong.";
                cmbInput.Focus();
                return;
            }
            cmbInput.Text = strEnc;

            strEnc = cmbOutputEnc.Text.Trim();
            if (strEnc == string.Empty)
            {
                txtOutput.Text = "Please select an encoding to convert.";
                cmbOutputEnc.Focus();
                return;
            }
            if (!cmbEncList.Items.Contains(strEnc))
            {
                txtOutput.Text = "output encoding wrong.";
                cmbOutputEnc.Focus();
                return;
            }
            cmbOutputEnc.Text = strEnc;

            convert();
        }

        private void convert()
        {
            string strTempFile;
            System.IO.StreamWriter swText;
            System.IO.StreamReader srText;

            string strCheck = txtInput.Text;

            if (strCheck.StartsWith("file:"))
            {
                strTempFile = strCheck.Substring(5);
            }
            else
            {
                strTempFile = Application.StartupPath + "\\temp.tmp";

                try
                {
                    swText = new System.IO.StreamWriter(strTempFile, false, Encoding.GetEncoding(cmbInput.Text));
                    swText.Write(txtInput.Text);
                    swText.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }

            try
            {
                if (!System.IO.File.Exists(strTempFile))
                {
                    MessageBox.Show("file not exist.");
                    return;
                }

                srText = new System.IO.StreamReader(strTempFile, Encoding.GetEncoding(cmbOutputEnc.Text), false);
                txtOutput.Text = srText.ReadToEnd();
                srText.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

        }
    }
}
